package pe.gob.sedapal.scmultimedia.rcp.view;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseAdapter;

import pe.gob.sedapal.scmultimedia.rcp.view.centrosservicios.ConsultaCentroServiciosView;
import pe.gob.sedapal.scmultimedia.rcp.view.estadocuenta.ConsultaEstadoCuentaView;
import pe.gob.sedapal.scmultimedia.rcp.view.servicios.ConsultaServiciosView;
import pe.gob.sedapal.scmultimedia.rcp.view.servicios.PdfView;
import pe.gob.sedapal.scmultimedia.util.Constante;
import pe.gob.sedapal.scmultimedia.util.Messages;
import pe.gob.sedapal.scmultimedia.wb.swt.ResourceManager;

public class PrincipalView extends ViewPart {
	
	public static final String ID = "scmultimedia.principalView";
	
	private StackLayout layout;
	private Composite cmpPrincipal;
	
	private Composite cmpEstados;
	private Composite cmpServicios;
	private Composite cmpCentros;

	public PrincipalView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(final Composite parent) {
		layout = new StackLayout();
		parent.setLayout(layout);
		
		parent.setBackgroundImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_FONDO_SEDAPAL));
		parent.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		cmpPrincipal = new Composite(parent,SWT.NONE);
		cmpPrincipal.setLayout(null);
		cmpPrincipal.setBackgroundImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_FONDO_SEDAPAL));
		cmpPrincipal.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		Label imgLogo = new Label(cmpPrincipal, SWT.NONE);
		imgLogo.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_SEDAPAL));
		imgLogo.setBounds(25, 35, 240, 95);
		
		Label imgConsulta = new Label(cmpPrincipal, SWT.NONE);
		imgConsulta.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_CONSULTA_PRINCIPAL));
		imgConsulta.setBounds(418, 162, 348, 382);
		
		Label txtConsulta = new Label(cmpPrincipal, SWT.NONE);
		txtConsulta.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(51, 102, 153));
		txtConsulta.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("DejaVu Sans", 20, SWT.BOLD));
		txtConsulta.setBounds(407, 77, 397, 26);
		txtConsulta.setText(Messages.principal_titulo);
		
		Label txtMultimedia = new Label(cmpPrincipal, SWT.NONE);
		txtMultimedia.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(51, 102, 153));
		txtMultimedia.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("DejaVu Sans", 20, SWT.BOLD));
		txtMultimedia.setBounds(495, 123, 249, 33);
		txtMultimedia.setText(Messages.principal_subtitulo);
		
		final Label btnServicios = new Label(cmpPrincipal, SWT.NONE);
		btnServicios.addMouseListener(new MouseAdapter() {//REPLICAR EN UN TOUCHLISTENER
			@Override
			public void mouseDown(MouseEvent e) {
				layout.topControl = cmpServicios;
				parent.layout();

				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IViewReference vistaActual = pagina.findViewReference(PrincipalView.ID);
				
				pagina.hideView(vistaActual);
				
				try{
					pagina.showView(ConsultaServiciosView.ID);
					pagina.showView(NavegacionView.ID);
					IViewReference vistaPDF  = pagina.findViewReference(PdfView.ID);
					pagina.hideView(vistaPDF);					
				}catch(Exception ex){
					ex.printStackTrace();
				}
				
				
			}
		});

		btnServicios.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnServicios.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_SERVICIOS_ON));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnServicios.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_SERVICIOS_ON));
			}
		});
		btnServicios.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_SERVICIOS_ON));
		btnServicios.setBounds(42, 427, 348, 128);

		final Label btnCentros = new Label(cmpPrincipal, SWT.NONE);
		btnCentros.addMouseListener(new MouseAdapter() {//REPLICAR EN UN TOUCHLISTENER
			@Override
			public void mouseDown(MouseEvent e) {
				layout.topControl = cmpCentros;
				parent.layout();
				
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IViewReference vistaActual = pagina.findViewReference(PrincipalView.ID);
				
				pagina.hideView(vistaActual);
				
				try{
					pagina.showView(ConsultaCentroServiciosView.ID);
					pagina.showView(NavegacionView.ID);
				}catch(Exception ex){
					ex.printStackTrace();
				}
				
			}
		});
		btnCentros.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnCentros.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_CENTROS_ON));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnCentros.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_CENTROS_ON));
			}
		});
		btnCentros.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_CENTROS_ON));
		btnCentros.setBounds(42, 293, 348, 128);
		
		
		
		final Label btnEstados = new Label(cmpPrincipal, SWT.NONE);
		btnEstados.addMouseListener(new MouseAdapter() {//REPLICAR EN UN TOUCHLISTENER
			@Override
			public void mouseDown(MouseEvent e) {

				
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IViewReference vistaActual = pagina.findViewReference(PrincipalView.ID);
				
				pagina.hideView(vistaActual);
				
				try{
					pagina.showView(ConsultaEstadoCuentaView.ID);
					pagina.showView(NavegacionView.ID);
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});

		btnEstados.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnEstados.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_SALDOS_ON));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnEstados.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_SALDOS_ON));
			}
		});
		btnEstados.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BOTON_SALDOS_ON));
		btnEstados.setBounds(42, 147, 348, 128);
		
		cmpEstados = new Composite(parent,SWT.NONE);
		cmpEstados.setLayout(null);
		final Button btn0 = new Button(cmpEstados,  SWT.BORDER | SWT.FLAT);
		btn0.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				layout.topControl = cmpPrincipal;
				parent.layout();
			}
		});
		btn0.setText("Regresar desde Estados");
		btn0.setBounds(73, 192, 252, 62);

		cmpServicios = new Composite(parent,SWT.NONE);
		cmpServicios.setLayout(null);
		final Button btn = new Button(cmpServicios,  SWT.BORDER | SWT.FLAT);
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				layout.topControl = cmpPrincipal;
				parent.layout();
			}
		});
		btn.setText("Regresar desde Servicios");
		btn.setBounds(73, 192, 252, 62);

		cmpCentros = new Composite(parent,SWT.NONE);
		cmpCentros.setLayout(null);
		final Button btn2 = new Button(cmpCentros,  SWT.BORDER | SWT.FLAT);
		btn2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				layout.topControl = cmpPrincipal;
				parent.layout();
			}
		});
				
		btn2.setText("Regresar desde Centros");
		btn2.setBounds(73, 192, 252, 62);
		
		layout.topControl = cmpPrincipal;
		parent.layout();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unused")
	private void runSubTask(IProgressMonitor monitor, int subTaskId) {

		 monitor.beginTask("Sub task running", 10);
		  for (int i = 0; i < 10; i++) {
		   monitor.subTask("Inside subtask, " + i + " out of 10");

			//INICIO DE TAREA 
			IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			IViewReference vistaActual = pagina.findViewReference(PrincipalView.ID);
						
			pagina.hideView(vistaActual);
			
			try{
				pagina.showView(ConsultaServiciosView.ID);
				IViewReference vistaPDF  = pagina.findViewReference(PdfView.ID);
				pagina.hideView(vistaPDF);					
			}catch(Exception ex){
				ex.printStackTrace();
			}
			//FIN DE TAREA

		   monitor.worked(1);
		   if (monitor.isCanceled())
		    throw new OperationCanceledException();
		 }
		  monitor.done();
	}
}
